<script>
	var url;
	$(document).ready(function(){
	
		newData = function (){
			$('#dialog').dialog({
				title: 'Tambah Kalender Akademik',
				width: 550,
				height: 600,
				closed: true,
				cache: false,
				href: base_url+'kalender_akademik/add',
				modal: true, 
				resizable:true
			});
			 
			$('#dialog').dialog('open');
			url = base_url+'kalender_akademik/save/add';
		}
		// end newData
		
		editData = function (id, kd_prodi){
			$('#dialog').dialog({
				title: 'Edit Kalender Akademik',
				width: 550,
				height: 625,
				closed: true,
				cache: false,
				href: base_url+'kalender_akademik/edit/'+id+'/'+kd_prodi,
				modal: true
			});
			
			$('#dialog').dialog('open');  
			url = base_url+'kalender_akademik/save/edit';
		}
		//end editData
		
		deleteData = function (id){
			if(confirm("Apakah yakin akan menghapus data dengan id '" + id + "'?")){
				var response = '';
				$.ajax({ type: "GET",
					 url: base_url+'kalender_akademik/delete/' + id,
					 async: false,
					 success : function(response){
						var response = eval('('+response+')');
						if (response.success){
							$.messager.show({
								title: 'Success',
								msg: 'Data Berhasil Dihapus'
							});
							$('#dg').datagrid('reload');
						} else {
							$.messager.show({
								title: 'Error',
								msg: response.msg
							});
						}
					 }
				});
			}
		}
		//end deleteData 
		
		saveData = function(){
			
			$('#form1').form('submit',{
				url: url,
				onSubmit: function(){
					return $(this).form('validate');
				},
				success: function(result){
					//alert(result);
					var result = eval('('+result+')');
					if (result.success){
						$('#dialog').dialog('close');		// close the dialog
						$('#dg').datagrid('reload');		// reload the user data
					} else {
						$.messager.show({
							title: 'Error',
							msg: result.msg
						});
					}
				}
			});
		}
		//end saveData
		
		actionbutton = function(value, row, index){
			var col;
			<?if($this->mdl_auth->CekAkses(array('menu_id'=>35, 'policy'=>'EDIT'))){?>
			col = '<a href="#" onclick="editData(\''+row.id_tahunajaran+'\', \''+row.kd_prodi+'\');" class="easyui-linkbutton" iconCls="icon-edit" plain="false">Edit</a>';
			<?}?>
			col += '&nbsp;&nbsp;|&nbsp;&nbsp;';
			<?if($this->mdl_auth->CekAkses(array('menu_id'=>35, 'policy'=>'DELETE'))){?>
			col += '<a href="#" onclick="deleteData(\''+row.id_tahunajaran+'\');" class="easyui-linkbutton" iconCls="icon-edit" plain="false">Delete</a>';
			<?}?>
			return col;
		}
		
		// load grid
		$(function(){
			$('#dg').datagrid({url:"<?=base_url()?>kalender_akademik/grid"});	
			//$('#dg').datagrid('enableFilter'); 			
		});	
		
		
		// onchange
		$('#s_kd_pt').change(function(){
			$('#s_kd_fakultas').load(base_url+'prosedur/getFakultasbyUniversitas/'+$('#s_kd_pt').val());
		});
		$('#s_kd_fakultas').change(function(){
			$('#s_kd_prodi').load(base_url+'prosedur/getProdibyFakultas/'+$('#s_kd_fakultas').val());
		});
		
		// filter
		filter = function(){
			$('#dg').datagrid('load',{
				kd_fakultas : $('#s_kd_fakultas').val(),
				kd_prodi : $('#s_kd_prodi').val(),
				kd_pt : $('#s_kd_pt').val()
			});
			//$('#dg').datagrid('enableFilter');
		}
		
		// detail grid
		$('#dg').datagrid({
			view: detailview,
			detailFormatter:function(index,row){
				return '<div width="1000px" style="padding:2px"><table id="ddv-' + index + '"></table></div>';
			},
			onExpandRow: function(index,row){
				$('#ddv-'+index).datagrid({
					url:base_url+'kalender_akademik/grid_detail/'+row.id_tahunajaran+'/'+row.kd_prodi,
					fitColumns:false,
					singleSelect:true,
					rownumbers:true,
					loadMsg:'',
					height:'auto',
					columns:[[
						{field:'TglKRSMulai',title:'KRS Mulai',width:80},
						{field:'TglKRSSelesai',title:'KRS Selesai',width:80},
						{field:'TglUbahKRSMulai',title:'PRS Mulai',width:80},
						{field:'TglUbahKRSSelesai',title:'PRS Selesai',width:80},
						{field:'TglKuliahMulai',title:'Kuliah Mulai',width:130},
						{field:'TglKuliahSelesai',title:'Kuliah Selesai',width:130},
						{field:'TglUTSMulai',title:'UTS Mulai',width:70},
						{field:'TglUTSSelesai',title:'UTS Selesai',width:70},
						{field:'TglUASMulai',title:'UAS Mulai',width:70},
						{field:'TglUASSelesai',title:'UAS Selesai',width:71}
					]],
					onResize:function(){
						$('#dg').datagrid('fixDetailRowHeight',index);
					},
					onLoadSuccess:function(){
						setTimeout(function(){
							$('#dg').datagrid('fixDetailRowHeight',index);
						},0);
					}
				});
				$('#dg').datagrid('fixDetailRowHeight',index);
			}
		});
		
		//# Tombol Bawah
		$(function(){
			var pager = $('#dg').datagrid().datagrid('getPager');	// get the pager of datagrid
			pager.pagination({
				buttons:[
					<?if($this->mdl_auth->CekAkses(array('menu_id'=>35, 'policy'=>'ADD'))){?>
					{
						iconCls:'icon-add',
						text:'Tambah Data',
						handler:function(){
							newData();
						}
					}
					<?}?>
				]
			});			
		});
		
	});	
		
</script>

<table id="dg" title="Kelola Kalender Akademik" data-options="
			rownumbers:true,
			singleSelect:true,
			autoRowHeight:false,
			pagination:true,
			pageSize:30,
			fit:true,
			cache:false,
			toolbar:'#toolbar'
			">		
	<thead>
		<tr>
			<th field="id_tahunajaran" sortable="true" width="100">Tahun Ajaran</th>
			<th field="Nama" sortable="true" width="200">Nama Tahun Ajaran</th>
			<th field="nama_pt" width="150">Perguruan Tinggi</th>
			<th field="nama_fakultas" width="200">Fakultas</th>
			<th field="nama_prodi" width="200">Program Studi</th>
			<th field="action" align="center" formatter="actionbutton" width="90">Aksi</th>
		</tr>
	</thead>
</table>
<div id="toolbar" style="padding:5px;height:auto">
	<div>
		<table width="700" border="0">
		  <tr>
			<td>Perguruan Tinggi</td>
			<td>: 
				<select id="s_kd_pt" name="s_kd_pt" style="width:200px;">
					<?=$this->mdl_prosedur->OptionUniversitas();?>
				</select>
			</td>
			<td>&nbsp;</td>
			<td>Fakultas</td>
			<td>: 
				<select id="s_kd_fakultas" name="s_kd_fakultas" style="width:200px;">
					<?=$this->mdl_prosedur->OptionFakultas();?>
				</select>
			</td>
		  </tr>
		  <tr>
			<td>&nbsp;</td>
			<td>&nbsp;&nbsp;<a href="#" onclick="filter()" class="easyui-linkbutton" iconCls="icon-search">Filter</a></td>
			<td>&nbsp;</td>
			<td>Program Studi</td>
			<td>: 
				<select id="s_kd_prodi" name="s_kd_prodi" style="width:200px;">
					<?=$this->mdl_prosedur->OptionProdi();?>
				</select>
			</td>
		  </tr>
		</table>
	</div>
</div>
